<?php
namespace app\admin\controller;

use think\Jwt;
use think\cache\driver\Redis;
use think\View;

class Base
{
    public function __construct ()
    {
        $jwt = Jwt::getInstance ();
        $this->uid = $jwt->get ('uid');
        if(empty($this->uid)) abort(404, '页面不存在');
        $menu = [
            ['name' => '新闻资讯', 'url' => '/news.html'],
            ['name' => '预订房间', 'url' => '/room.html'],
            ['name' => '菜单明细', 'url' => '/food.html'],
            ['name' => '旅游攻略', 'url' => '/strategy.html'],
        ];
        View::share ('webConfig', [
            'title'       => '后台-溪边居民宿-后岸农家乐69号',
            'description' => '溪边居农家乐',
            'keywords'    => '后岸,农家乐,溪边居,民宿,天台街头',
            'static_url'  => '',
        ]);
        View::share ('menu_list', $menu);
        $userInfo = $this->getUserInfo ();
        View::share ('userInfo', $userInfo);
    }

    private function getUserInfo ()
    {
        $redis = new Redis();
        if (empty($date)) $date = date ('Y-m-d', time () - 86400);
        $startTime = strtotime ($date);
        $endTime = $startTime + 86399;
        $userInfo = $redis->hGet ('ADMIN_USER_INFO', $date);
        $userInfo = json_decode ($userInfo, true);
        if (empty($userInfo)) {
            //获取昨日访日人数
        }
        return $userInfo;
    }

    protected function ajaxMsg ($msg = '', $status = '200')
    {
        if (is_array ($msg)) {
            if (empty($msg['status'])) $msg['status'] = $status;
            $data = $msg;
        } elseif (is_string ($msg)) {
            $data = ['message' => $msg, 'status' => $status];
        } else {
            $data = ['message' => '不支持返回类型', 'status' => '500'];
        }
        echo json_encode ($data);
    }
}
